package artid.com.arttogoa.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Toast;

import artid.com.arttogoa.R;
/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：GoodatAdapter   
 * 类描述   ：学校适配器   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-4 上午10:27:57   
 *
 */
public class GoodatAdapter extends BaseAdapter {
	private ArrayList<HashMap<String, Object>> lstImageItem;
	private List<String> list;
	private Context context;

	public GoodatAdapter(Context mContext,ArrayList<HashMap<String, Object>> lstImageItem) {
		context = mContext;
		this.lstImageItem = lstImageItem;
	}

	public GoodatAdapter(List<String> list, Context mContext,ArrayList<HashMap<String, Object>> lstImageItem) {
		super();
		this.list = list;
		context = mContext;
		this.lstImageItem = lstImageItem;
	}




	@Override
	public int getCount() {
		return lstImageItem.size();
	}

	@Override
	public Object getItem(int position) {
		return lstImageItem.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = View.inflate(context, R.layout.item_good_at, null);
			vh.cb = (CheckBox) convertView.findViewById(R.id.guohua);
			vh.cb.setClickable(false);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		if(list !=null && list.size()!=0){
			for(int i=0;i<list.size();i++){
				if(list.get(i).toString().equals(lstImageItem.get(position).get("goodat"))){
					vh.cb.setChecked(true);
					checkedArray.put(position, list.get(i).toString());
				}
			}
		}
		final String string = String.valueOf(lstImageItem.get(position).get("goodat"));
		vh.cb.setText(string);
		final ViewHolder finalVh = vh;
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean checked = finalVh.cb.isChecked(); // 如果没选第一次石轩中、
				Log.e("convertView", "checkbox state = " +position+" - - "+ checked);
				checked = !checked;
				Log.e("convertView", "checkbox state = " +position+" - - "+ checked);
				if (checked) {
					if(checkedArray.size() >= 3){
						Log.e("convertView", "选中超过3个");
						Toast.makeText(context, "擅长领域不能超过3个~", Toast.LENGTH_LONG).show();
						finalVh.cb.setChecked(false);
						return;
					}
					Log.e("convertView", "选中");
					finalVh.cb.setChecked(true);
					checkedArray.put(position, string);
				} else {
					Log.e("convertView", "取消选中");
					checkedArray.remove(position);
					finalVh.cb.setChecked(false);
				}
				Intent intent0 = new Intent("com.ImproveInformationActivity.receiver");  
				context.sendBroadcast(intent0);
				Intent intent2 = new Intent("com.EditMessageActivity.receiver");  
				context.sendBroadcast(intent2);
			}
		});
		return convertView;
	}

	public String getCheckedVal() {
		String val = "";
		Set<Integer> keySet = checkedArray.keySet();
		for(Integer key : keySet) {
			if(!StringUtils.isEmpty(val)){
				val += ",".concat(checkedArray.get(key));
			}else{
				val=checkedArray.get(key);
			}
		}
		return val;
	}


	private LinkedHashMap<Integer, String> checkedArray = new LinkedHashMap<Integer, String>(3); 

	class ViewHolder {
		CheckBox cb;
		String bo;
	}

}
